package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    /**
     * 发布视频
     *  接口路径：POST
     *  请求参数：
     *      videoThumbnail：封面图
     *      videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1")  Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 小视频点赞
     * @param id 小视频id
     * @return
     */
    @PostMapping("/{id}/like")
    public ResponseEntity videoLike(@PathVariable("id")String id){
        videosService.addLike(id);
        return null;
    }

    /**
     * 取消小视频点赞
     * @param id 小视频id
     * @return
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity videoDisLike(@PathVariable("id") String id){
        videosService.videoDisLike(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询视频评论列表
     * @param page 第几页
     * @param pageSize 每页显示个数
     * @param id 小视频id
     * @return
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity findVideoComments(@RequestParam(defaultValue = "1") Integer page,
                                            @RequestParam(defaultValue = "10")Integer pageSize,
                                            @PathVariable("id")String id){
        PageResult pr = videosService.findVideoComments(page,pageSize,id);
        return ResponseEntity.ok(pr);
    }

    /**
     * 评论视频
     * @param id 小视频id
     * @param map 评论的内容
     * @return
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity videoAddComments(@PathVariable("id") String id, @RequestBody Map map){
        videosService.videoAddComments(id,map);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户关注
     * @param uid 关注用户的id
     * @return
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid")Long uid){
        videosService.userFocus(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 取消关注
     * @param uid 当前小视频发布者id
     * @return
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid")Long uid){
        videosService.userUnFocus(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 评论点赞
     * @param id 评论的id
     * @return
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity commentsLike(@PathVariable("id") String id){
        videosService.commentsLike(id);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity commentsDisLike(@PathVariable("id") String id){
        videosService.commentsDisLike(id);
        return ResponseEntity.ok(null);
    }
}